# -*- coding: utf-8 -*-

from gtk import *
from includes import *
from gobject import *
from photo import *
from threading import *
from actions import *

class Browser(HBox):
	def __init__(self, ppm):
		HBox.__init__(self)
		self.ppm = ppm
		
		stock_items = (
		("gtk-add-photos", "_Ajouter des photos", 0, 0, None),
		("gtk-remove-photos", "_Enlever la sélection", 0, 0, None),
				)

		stock_aliases = (
		("gtk-add-photos", STOCK_ADD),
		("gtk-remove-photos", STOCK_REMOVE),
				)

		stock_add(stock_items)

		factory = IconFactory()
		factory.add_default()

		for item, alias in stock_aliases:
			icon_set = icon_factory_lookup_default(alias)
			factory.add(item, icon_set)
		
		
		
		self.actions_selection = HButtonBox()
		self.actions_selection.set_spacing(5)
		self.actions_selection.set_layout(BUTTONBOX_START)
		
		add = Button(stock="gtk-add-photos")
		add.connect("clicked", self.add_dialog)
		self.actions_selection.add(add)
		remove = Button(stock="gtk-remove-photos")
		remove.connect("clicked", self.remove)
		self.actions_selection.add(remove)
		
		
		self.actions = Notebook()
		self.actions.set_show_border(False)
		self.actions.set_show_tabs(False)
		
		self.actions.append_page(self.actions_selection)
		
		self.actions_format = HBox()
		self.actions_format.set_spacing(5)
		
		self.actions_format.pack_start(Label("Lignes :"))
		self.lignes = SpinButton(adjustment=Adjustment(value=1, lower=1, upper=100, step_incr=1, page_incr=10, page_size=0))
		self.lignes.connect("value-changed", ppm.photoformat.changed)
		self.actions_format.pack_start(self.lignes)
		
		self.actions_format.pack_start(Label("Colonnes :"))
		self.colonnes = SpinButton(adjustment=Adjustment(value=1, lower=1, upper=100, step_incr=1, page_incr=10, page_size=0))
		self.colonnes.connect("value-changed", ppm.photoformat.changed)
		self.actions_format.pack_start(self.colonnes)
		
		self.actions.append_page(self.actions_format)
		self.actions.append_page(Label("Actions 3"))
		
		self.pack_start(self.actions, padding=5)
		
		nav = HButtonBox()
		self.previous = Button(stock=STOCK_GO_BACK)
		self.previous.connect("clicked", ppm.previous)
		self.previous.set_sensitive(False)
		nav.add(self.previous)
		self.next = Button(stock=STOCK_GO_FORWARD)
		self.next.connect("clicked", ppm.next)
		nav.add(self.next)
		nav.set_spacing(5)
		nav.set_layout(BUTTONBOX_END)
		
		self.pack_start(nav, padding=5)
	
	
	def update_step(self, step):
		self.previous.set_sensitive(step != STEP_SELECTION)
		self.actions.set_current_page(step)
		if step == STEP_CROPPING:
			self.next.set_label(STOCK_PRINT)
		else:
			self.next.set_label(STOCK_GO_FORWARD)
		
	def add_dialog(self, button):
		filechooser = FileChooserDialog(title="Sélectionnez un ou plusieurs dossiers à importer", action=FILE_CHOOSER_ACTION_SELECT_FOLDER, parent=self.ppm, buttons=(STOCK_CANCEL, RESPONSE_CANCEL, STOCK_ADD, RESPONSE_OK))
		filtre = FileFilter()
		filtre.add_pixbuf_formats()
		filechooser.set_filter(filtre)
		filechooser.set_select_multiple(True)
		filechooser.set_local_only(False)
		filechooser.set_show_hidden(False)
		
		preview = Image()
		filechooser.set_preview_widget(preview)
		filechooser.connect("update-preview", update_preview, preview)
		
		check_folder = CheckButton("Sélectionner directement les photos plutôt qu'un ou plusieurs dossiers")
		check_folder.connect("toggled", update_chooser_type, filechooser)
		filechooser.set_extra_widget(check_folder)
		
		home = expanduser("~")
		interresting_dirs = ("Photos", "photos", "Images", "images", "")
		i = 0
		while not isdir(join(home, interresting_dirs[i])):
			i += 1
		filechooser.set_current_folder(join(home, interresting_dirs[i]))
		
		if filechooser.run() == RESPONSE_OK:
			self.ppm.create_handler()
			self.ppm.handler.add_photos(filechooser.get_filenames())
			self.ppm.start_handler()
		filechooser.destroy()
	
	def remove(self, button):
		self.ppm.create_handler()
		self.ppm.handler.remove_photos(self.ppm.photoview.iv.get_selected_items())
		self.ppm.start_handler()
		
	
def update_preview(filechooser, preview):
	filename = filechooser.get_preview_filename()
	try:
		preview.set_from_pixbuf(gdk.pixbuf_new_from_file_at_size(filename, 128, 128))
		have_preview = True
	except GError:
		have_preview = False
	except TypeError:
		have_preview = False
	filechooser.set_preview_widget_active(have_preview)
	
def update_chooser_type(checkbutton, filechooser):
	if checkbutton.get_active():
		filechooser.set_action(FILE_CHOOSER_ACTION_OPEN)
		filechooser.set_title("Sélectionnez les photos à importer")
	else:
		filechooser.set_action(FILE_CHOOSER_ACTION_SELECT_FOLDER)
		filechooser.set_title("Sélectionnez un ou plusieurs dossiers à importer")
	

